package castledefense.gui.upgradePanel;

import twl.global.widget.LabelFonts;
import castledefense.data.BuildingData;
import castledefense.data.GameData;
import castledefense.logic.Map;
import castledefense.logic.buildings.Building;
import castledefense.logic.buildings.Castle;
import castledefense.logic.buildings.Tower1;
import castledefense.logic.buildings.Tower2;
import de.matthiasmann.twl.Widget;
import de.matthiasmann.twl.renderer.AnimationState.StateKey;

public class BuildingInfoPanel extends Widget{
	
	public static final StateKey STATE_TOWER1 = StateKey.get("tower1");
	public static final StateKey STATE_TOWER2 = StateKey.get("tower2");
	public static final StateKey STATE_GATE = StateKey.get("gate");
	public static final StateKey STATE_CASTLE = StateKey.get("castle");
	
	public static final StateKey STATE_FIRE = StateKey.get("fire");
	public static final StateKey STATE_WATER = StateKey.get("water");
	public static final StateKey STATE_EARTH = StateKey.get("earth");
	public static final StateKey STATE_AIR = StateKey.get("air");
	
	private Widget buildingImage;
	private Widget effectImage;
	private LabelFonts labelTitle;
	private LabelFonts labelLevel;
	private LabelFonts labelDescription;
	private LabelFonts labelDamage;
	private LabelFonts labelRange;
	private LabelFonts labelCost;
	private LabelFonts labelElement;
	private LabelFonts labelEffect;
	
	public BuildingInfoPanel() {
		//BuildingButtonPanel
		int width = (int) (Map.cellWidth * 1.5);
		int height = (int) (Map.cellWidth * 1.5);
		width = 45;
		height = 45;

		setTheme("");
		
		buildingImage = new Widget();
		buildingImage.setTheme("buildingIconTheme");
		buildingImage.setSize(width, height);
		buildingImage.setPosition(10, 10);
		add(buildingImage);

		labelTitle = new LabelFonts();
		labelTitle.setTheme("labelTheme");
		labelTitle.setFont("arialBold20");
		labelTitle.setPosition(62, 20);
		add(labelTitle);

		labelLevel = new LabelFonts();
		labelLevel.setTheme("labelTheme");
		labelLevel.setFont("arialBoldItalic16");
		labelLevel.setColor(LabelFonts.STATE_BLACK);
		labelLevel.setPosition(62, 42);
		add(labelLevel);
		
		Widget line = new Widget();
		line.setTheme("fillRectBlack");
		line.setSize(159, 1);
		line.setPosition(10, 60);
		add(line);
		
		labelDescription = new LabelFonts();
		labelDescription.setTheme("labelTheme");
		labelDescription.setFont("arialItalic16");
		labelDescription.setPosition(10, 80);
		add(labelDescription);
		
		
		
		LabelFonts damage = new LabelFonts("Damage");
		damage.setTheme("labelTheme");
		damage.setFont("arialBold16");
		damage.setPosition(10, 110);
		add(damage);
		
		LabelFonts range = new LabelFonts("Range");
		range.setTheme("labelTheme");
		range.setFont("arialBold16");
		range.setPosition(10, 125);
		add(range);
		
		LabelFonts cost = new LabelFonts("Cost");
		cost.setTheme("labelTheme");
		cost.setFont("arialBold16");
		cost.setPosition(10, 140);
		add(cost);
		
		LabelFonts element = new LabelFonts("Element");
		element.setTheme("labelTheme");
		element.setFont("arialBold16");
		element.setPosition(10, 155);
		add(element);
		
		LabelFonts effect = new LabelFonts("Effect");
		effect.setTheme("labelTheme");
		effect.setFont("arialBold16");
		effect.setPosition(10, 170);
		add(effect);
		
		labelDamage = new LabelFonts("Damage");
		labelDamage.setTheme("labelTheme");
		labelDamage.setFont("arialBold15");
		labelDamage.setPosition(70, 110);
		add(labelDamage);
		
		labelRange = new LabelFonts("Range");
		labelRange.setTheme("labelTheme");
		labelRange.setFont("arialBold15");
		labelRange.setPosition(70, 125);
		add(labelRange);
		
		labelCost = new LabelFonts("Cost");
		labelCost.setTheme("labelTheme");
		labelCost.setFont("arialBold15");
		labelCost.setPosition(70, 140);
		add(labelCost);
		
		labelElement = new LabelFonts();
		labelElement.setTheme("labelTheme");
		labelElement.setFont("arialBold15");
		labelElement.setPosition(70, 155);
		add(labelElement);
		
		labelEffect = new LabelFonts();
		labelEffect.setTheme("labelTheme");
		labelEffect.setFont("arialBold15");
		labelEffect.setPosition(70, 170);
		add(labelEffect);
		
		effectImage = new Widget();
		effectImage.setTheme("elementIconTheme");
		effectImage.setPosition(80, 162);
		effectImage.setSize(16, 16);
		add(effectImage);
		
	}
	
	public void draw(Building building, Widget rootPane) {
		
		if(building instanceof Tower1){
			Tower1 tower1 = (Tower1)building;
			labelTitle.setText("Tower1");
			labelLevel.setText("(Level " + tower1.getUpgradeLevel(tower1.getElementType()) + ")");
			labelDescription.setText("A basic tower that attack a\nsingle target.");
			labelDamage.setText(":  " + tower1.getDamage());
			labelRange.setText(":  " + tower1.getRange());
			labelCost.setText(":  " + BuildingData.getCost(BuildingData.tower1) + "$");
		}else if(building instanceof Tower2){
			Tower2 tower2 = (Tower2)building;
			labelTitle.setText("Tower2");
			//labelHotkey.setText("(Hotkey = " + button.getHotkey() + ")");
			labelDescription.setText("A short range tower that\nattack every enemy around.");
			labelDamage.setText(":  " + tower2.getDamage());
			labelRange.setText(":  " + tower2.getRange());
			labelCost.setText(":  " + BuildingData.getCost(BuildingData.tower2) + "$");
		}
		
		effectImage.getAnimationState().setAnimationState(STATE_FIRE, building.getElementType() == GameData.fire);
		effectImage.getAnimationState().setAnimationState(STATE_WATER, building.getElementType() == GameData.water);
		effectImage.getAnimationState().setAnimationState(STATE_EARTH, building.getElementType() == GameData.earth);
		effectImage.getAnimationState().setAnimationState(STATE_AIR, building.getElementType() == GameData.air);
		
		if(building.getElementType() == GameData.fire){
			labelElement.setText(":  Fire");
			labelEffect.setText(": ");
		}else if(building.getElementType() == GameData.water){
			labelElement.setText(":  Water");
			labelEffect.setText(": ");
		}else if(building.getElementType() == GameData.earth){
			labelElement.setText(":  Earth");
			labelEffect.setText(": ");
		}else if(building.getElementType() == GameData.air){
			labelElement.setText(":  Air");
			labelEffect.setText(": ");
		}else{
			labelElement.setText(":  --");
			labelEffect.setText(":  --");
		}
		
		buildingImage.getAnimationState().setAnimationState(STATE_TOWER1, building instanceof Tower1);
		buildingImage.getAnimationState().setAnimationState(STATE_TOWER2, building instanceof Tower2);
		buildingImage.getAnimationState().setAnimationState(STATE_CASTLE, building instanceof Castle);
		
		if(!(rootPane.getNumChildren() == 1 && rootPane.getChild(0) == this)){
			rootPane.removeAllChildren();
			rootPane.add(this);
		}
	}

}
